// 简单的路由系统
import Home from '../pages/Home.js';
import Features from '../pages/Features.js';
import UseCases from '../pages/UseCases.js';
import Technology from '../pages/Technology.js';
import Download from '../pages/Download.js';
import Changelog from '../pages/Changelog.js';
import GettingStarted from '../pages/GettingStarted.js';
import FAQ from '../pages/FAQ.js';
import About from '../pages/About.js';

const routes = [
  {
    path: '/',
    component: Home,
  },
  {
    path: '/features',
    component: Features,
  },
  {
    path: '/use-cases',
    component: UseCases,
  },
  {
    path: '/technology',
    component: Technology,
  },
  {
    path: '/download',
    component: Download,
  },
  {
    path: '/changelog',
    component: Changelog,
  },
  {
    path: '/getting-started',
    component: GettingStarted,
  },
  {
    path: '/faq',
    component: FAQ,
  },
  {
    path: '/about',
    component: About,
  },
];

export function navigateTo(path) {
  window.history.pushState({}, '', path);
  renderRoute();
}

export function renderRoute() {
  const path = window.location.pathname;
  let component = Home;
  
  for (const route of routes) {
    if (route.path === path) {
      component = route.component;
      break;
    }
  }
  
  const app = document.getElementById('app');
  app.innerHTML = '';
  app.appendChild(component());
  
  // 更新导航高亮
  const navLinks = document.querySelectorAll('nav a');
  navLinks.forEach(link => {
    link.classList.remove('text-primary');
    link.classList.add('text-gray-600');
    if (link.getAttribute('href') === path) {
      link.classList.remove('text-gray-600');
      link.classList.add('text-primary');
    }
  });
}

// 监听浏览器历史变化
window.addEventListener('popstate', renderRoute);

// 初始化路由
window.addEventListener('DOMContentLoaded', renderRoute);